package com.gimi.cloud.bbp.gateway.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.bbp.common.constant.RedisKeyConstant;
import com.gimi.cloud.bbp.commonService.redis.RedisService;
import com.gimi.cloud.bbp.dao.dao.sys.SysMasterDao;
import com.gimi.cloud.bbp.dto.sys.SysMasterDTO;
import com.gimi.cloud.bbp.gateway.service.CheckMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户校验相关service实现类
 * @author  wbj
 * @date 2017-01-20
 * @version  1.0
 */
@Service
public class CheckMasterServiceImpl implements CheckMasterService {
    private final SysMasterDao masterDao;
    private final RedisService redisService;

    @Autowired
    public CheckMasterServiceImpl(SysMasterDao masterDao,RedisService redisService){
        this.masterDao = masterDao;
        this.redisService = redisService;
    }

    @Override
    public SysMasterDTO getMasterByUserName(String userName) {
        String redisKey = RedisKeyConstant.MANAGE_LOGIN_MASTER_PREFIX + userName;
        //获取redis存储的用户信息
        SysMasterDTO result = redisService.getObjectByKey(redisKey,SysMasterDTO.class);
        if(null == result){
            result = masterDao.findByUserName(userName);
            if(result != null){
                //如果用户信息不存在，则直接存在到redis中
                redisService.setObject(redisKey,result);
            }
        }
        return result;
    }
}
